import { env } from '@/shared/constants';
import { CompanyDto, ListRatingDto, StaffDto } from '@/shared/interfaces/generated';
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';

export const companyApi = createApi({
  reducerPath: 'companyApi',
  baseQuery: fetchBaseQuery({
    baseUrl: env.apiUrl,
    prepareHeaders: async (headers, query) => {
      const authResult = await (query.getState() as { auth: any })?.auth;
      if (authResult && authResult.accessToken) {
        headers.set('authorization', `Bearer ${authResult.accessToken}`);
      }
      return headers;
    },
  }),
  endpoints: (builder) => ({
    getCompany: builder.query<CompanyDto, string | number>({
      query: (id) => ({
        url: `/companies/${id}`,
      }),
    }),
    getCompanyRatings: builder.query<ListRatingDto, string | number>({
      query: (id) => ({
        url: `/companies/${id}/productRatings`,
      }),
    }),
    getCompanyStaff: builder.query<{ staff: StaffDto[] }, string | number>({
      query: (id) => ({
        url: `/companies/${id}/staff`,
      }),
    }),
  }),
});
